//
//  RECheckLogin.m
//  录音
//
//  Created by ywy on 2016/12/22.
//  Copyright © 2016年 Wely. All rights reserved.
//

#import "RECheckLogin.h"

@implementation RECheckLogin

+(BOOL)checkLogin
{
// userid
   NSString * userid =   [UserDefaults objectForKey:@"userid"];
    
    if (userid) {
        return YES;
    }else
    {
        return NO;
    }
    
}
+ (void)POST:(NSString *)URLString parameters:(id)parameters progress:(void (^)(CGFloat))progress success:(void (^)(id))sucess failure:(void (^)(NSError *))failure
{
        NSDictionary * dic = @{@"json":[RECheckLogin jsonDicToString:parameters]};
    
    //设置网络请求超时时间
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.securityPolicy = securityPolicy;
//    manager.requestSerializer.timeoutInterval = 10;
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json",nil];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:URLString parameters:dic progress:^(NSProgress * _Nonnull uploadProgress)
     {
         // uploadProgress.completedUnitCount/uploadProgress.totalUnitCount;
         CGFloat  prosser = uploadProgress.fractionCompleted;
         progress(prosser );
         
     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         //如果请求成功的话将responseObject保存在sucess Block中
         
         if (sucess)
         {
//             NSDictionary *jsonObject=[NSJSONSerialization
//                                       JSONObjectWithData:responseObject
//                                       options:NSJSONReadingMutableContainers
//                                       error:nil];
             
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
             sucess(dict);
             
         }
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         
         if(failure)
         {
             failure(error);
         }
         
     }];
}

+ (void)GET:(NSString *)URLString parameters:(id)parameters progress:(void (^)(CGFloat))progress success:(void (^)(id))sucess failure:(void (^)(NSError *))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    manager.securityPolicy = securityPolicy;
    manager.requestSerializer.timeoutInterval = 10;

    
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        CGFloat  prosser = downloadProgress.fractionCompleted;
                 progress(prosser );
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (sucess)
                     {
                         sucess(responseObject);
                     }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if(failure)
                     {
                         failure(error);
                     }
    }];
}
// 设置时间戳  获取时间
+ (NSString *)stringFromDate:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-dd"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    return destDateString;
}
// 是设置文件路径的时间戳
+(NSString  *)stringFromDateTwo:(NSDate*)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    return destDateString;
}
+(NSString  *)stringFromDateFirst:(NSDate*)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    return destDateString;
}
+(NSString*)getDataStr:(NSString*)dataStr andData:(NSDate*)date;
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    
    return [NSString stringWithFormat:@"%@-%@",destDateString,dataStr];
}

+(NSString *)jsonDicToString:(NSMutableDictionary*)dict
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}


+ (NSString *)imageBase64WithDataURL:(UIImage *)image
{
    NSData *imageData =nil;
    NSString *mimeType =nil;
    //图片要压缩的比例，此处100根据需求，自行设置
    CGFloat x =100 / image.size.height;
    if (x >1)
    {
        x = 1.0;
    }
    imageData = UIImageJPEGRepresentation(image, x);
    
    mimeType = @"image/jpeg";
// data:%@;base64,
    return [NSString stringWithFormat:@"%@",
            [imageData base64EncodedStringWithOptions:0]];
}
// 分割字符串
+(NSString*)segmentationString:(NSString*)character andAllStr:(NSString*)allStr
{
    NSArray *array = [allStr componentsSeparatedByString:character]; //从字符A中分隔成2个元素的数组
    return array[0];
}
+ (BOOL) isValidateMobile:(NSString *)mobile {
    //手机号以13、15、17、18开头，八个\d数字字符
    NSString *phoneRegex = @"^((13[0-9])|(147)|(15[^4,\\D])|(17[0-1,6-8])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phonePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phonePredicate evaluateWithObject:mobile];
}

+(NSString*)stringFromDateStr:(NSString *)dateStr withAdd:(BOOL)isAdd
{
    NSString *destDateString;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-dd"];
    if (isAdd) {
         NSDate* newDate  =  [dateFormatter dateFromString:dateStr];
         NSTimeInterval interval = 60 * 60 * 24;
        destDateString = [dateFormatter stringFromDate:[newDate initWithTimeInterval:interval sinceDate:newDate]];
    }else
    {
        NSDate* newDate  =  [dateFormatter dateFromString:dateStr];
        NSTimeInterval interval = 60 * 60 * 24;
       destDateString = [dateFormatter stringFromDate:[newDate initWithTimeInterval:-interval sinceDate:newDate]];
    }
    return destDateString;
}

+(NSString *)getlocalPath:(NSString*)str allStr:(NSString*)allPath;
{
    NSString*documentPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
    NSString * basePath = [allPath componentsSeparatedByString:str].lastObject;
    
    return  [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",basePath]];

}


+(void)POSTTwo:(NSString *)URLString parameters:(id)parameters withFormData:(void (^)(id))form progress:(void (^)(CGFloat))progress success:(void (^)(id))sucess failure:(void (^)(NSError *))failure{
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    manager.securityPolicy = securityPolicy;
    manager.requestSerializer.timeoutInterval = 10;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json",@"text/javascript",nil];
    
    [manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
   
        form(formData);
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (sucess)
        {
            sucess(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if(failure)
        {
            failure(error);
        }
    }];
    
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  
//    - (NSURLSessionDataTask *)POST:(NSString *)URLString
//parameters:(id)parameters
//constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
//progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress
//success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
//failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure

}



+(NSString * )getDocumentPath:(NSString*)pathStr
{
    NSArray *array =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documents = [array lastObject];
    NSString *documentPath = [documents stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.text",pathStr]];
    return documentPath;
}

+(NSString*)getDocumentPathWithStr:(NSString *)pathStr
{
    NSString*documentPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
    return  [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",pathStr]];

}

@end
